package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.dept;
import www.lagou.entity.employee;
import www.lagou.utils.DruidUtils;

import javax.management.Query;
import java.sql.ClientInfoStatus;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class employeeDao {
    public List<employee> findAllEmployee() throws SQLException {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //sql语句
        String sql = "select * from employee where did is not null";

        List<employee> employeeList = qr.query(sql, new BeanListHandler<employee>(employee.class));

        return employeeList;
    }

    public String getDeptName(int did) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from dept where id = ?";

        dept dept = qr.query(sql, new BeanHandler<dept>(dept.class), did);

        String deptname = dept.getDeptname();


        return deptname;
    }

    public List getEmployeeInfo() throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from employee";

        List<employee> query = qr.query(sql, new BeanListHandler<employee>(employee.class));

        List list = new ArrayList();

        for(employee e:query){
            int did = e.getDid();
            List employeeKeyInfo = null;
            String deptName = null;
            int id = e.getId();
            if(did > 0){
                 employeeKeyInfo = getEmployeeKeyInfo(did);
                 deptName = getDeptName(did);

            }else{
                employeeKeyInfo = getEmployeeKeyInfoPlus(id);
                deptName = null;
            }
            list.add(employeeKeyInfo);
            list.add(deptName);
        }

        return list;
    }

    public List getEmployeeKeyInfo(int did) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select Name ,salary from employee where did = ?";

        employee employee = qr.query(sql, new BeanHandler<employee>(employee.class), did);

        String name = employee.getName();
        double salary = employee.getSalary();

        List list = new ArrayList();
        list.add(name);
        list.add(salary);

        return list;
    }

    public List getEmployeeKeyInfoPlus(int id) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select Name,salary from employee where id = ?";

        employee query = qr.query(sql, new BeanHandler<employee>(employee.class), id);

        String name = query.getName();

        double salary = query.getSalary();

        List list2 = new ArrayList();

        list2.add(name);
        list2.add(salary);

        return list2;
    }

}
